const express = require('express');
const { pool } = require('../shamall/db');
const { authenticateToken } = require('../middleware/authentk');
const router = express.Router();

// 存储地址和坐标
router.post('/save-location', authenticateToken, async (req, res) => {
    const userId = req.user.id;
    const { address, latitude, longitude } = req.body;
    if (!address || latitude == null || longitude == null) {
        return res.status(400).json({
            code:400,
            msg:'缺少参数'
        });
    }
    try {
        const sql = 'INSERT INTO locations (user_id, address, latitude, longitude) VALUES (?, ?, ?, ?)';
        await pool.query(sql, [userId, address, latitude, longitude]);
        res.json({
            code: 200,
            msg: '保存成功'
        })
    } catch (err) {
        res.status(500).json({
            code:500,
            msg: '保存失败',
            error: err.message
        });
    }
})

module.exports = router;